package com.servertest1;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.EditText;
import android.widget.Toast;

public class NewGame extends Activity implements OnClickListener
{
	private Button searchByOpponent;
	private Client client;
	private DatabaseHelper helper;
	private LinearLayout random, recent;
	private String name;
	private EditText usernameToSearchFor;	
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.newgame);
		
		helper = new DatabaseHelper(this);
		name = helper.getUser();
		helper.close();
		
		random = (LinearLayout) findViewById(R.id.random);
		recent = (LinearLayout) findViewById(R.id.recent);
		
		random.setOnClickListener(new OnClickListener() {

		      public void onClick(View v) {
		    	  new RandomOpponentTask().execute("random_opponent", name);
		      }
		});

		
		usernameToSearchFor = (EditText) findViewById(R.id.usernameForSearch);
		
		searchByOpponent = (Button) findViewById(R.id.searchByUser);
		searchByOpponent.setOnClickListener(this);
		client = new Client();
	}
	
	@Override
	protected void onDestroy() {
	    super.onDestroy();
	    if (helper != null) {
	        helper.close();
	    }
	}

	public void onClick(View v) 
	{
		switch(v.getId())
		{

			case R.id.searchByUser:
				System.out.println("Clicked search by user button");
				helper = new DatabaseHelper(this);
				String you = helper.getUser();
				helper.close();
				String them = usernameToSearchFor.getText().toString();
				if(them.equals(you)) {
					Toast.makeText(NewGame.this, "You can't start a game with yourself", Toast.LENGTH_LONG).show();
				}
				else {
					new opponentByUser().execute("input_opponent", you, them);
				}
				break;
			default:
				break;
		}
	}

	private class opponentByUser extends AsyncTask<String, Void, String[]>{

		protected String[] doInBackground(String... cmd) {
			
			client.connect();
			client.sendData(cmd);
			String result[] = client.recieveData();
			client.close();
			
			return result;
		}

		protected void onPostExecute(String[] result){
			if(result[0].equals("player_search_game_success")){
				Intent goBack = new Intent();
				
				goBack.putExtra("created_game", result);

				setResult(RESULT_OK, goBack);
				
				finish();
			}
			else{
				Toast.makeText(NewGame.this, "An Error has occered.  Try again.", Toast.LENGTH_LONG).show();
			}
		}
	}
	
	private class RandomOpponentTask extends AsyncTask<String, Void, String[]>{

		protected String[] doInBackground(String... cmd) {
			
			client.connect();
			client.sendData(cmd);
			String result[] = client.recieveData();
			client.close();
			
			return result;
		}

		protected void onPostExecute(String[] result){
			if(result[0].equals("started_search")){
				Intent goBack = new Intent();
				
				goBack.putExtra("created_game", result);

				setResult(RESULT_OK, goBack);
				
				finish();
			}
			else if(result[0].equals("already_searching")){
				Toast.makeText(NewGame.this, "You are already searching for a random opponent.", Toast.LENGTH_LONG).show();
			}
			else{
				Toast.makeText(NewGame.this, "An Error has occered.  Try again.", Toast.LENGTH_LONG).show();
			}
		}
	}
}
